package com.sinoeyes.compent.service.impl;

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.utils.DateUtils;
import com.sinoeyes.common.utils.SnakeFlowUtil;
import com.sinoeyes.common.utils.StringUtils;
import com.sinoeyes.compent.mapper.CurrencyModelMapper;
import com.sinoeyes.compent.po.CurrencyModel;
import com.sinoeyes.compent.service.ICurrencyModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用于初版模板的CURD管理Service业务层处理
 *
 * @author aries.hao
 * @date 2019-09-05
 */
@Service
public class CurrencyModelServiceImpl implements ICurrencyModelService {

    @Autowired
    private CurrencyModelMapper currencyModelMapper;

    /**
     * 查询用于初版模板的CURD管理
     *
     * @param id 用于初版模板的CURD管理ID
     * @return 用于初版模板的CURD管理
     */
    @Override
    public CurrencyModel selectCurrencyModelById(String id) {
        return currencyModelMapper.selectCurrencyModelById(id);
    }

    /**
     * 查询用于初版模板的CURD管理列表
     *
     * @param CurrencyModel 用于初版模板的CURD管理
     * @return 用于初版模板的CURD管理
     */
    @Override
    public List<CurrencyModel> selectCurrencyModelList(CurrencyModel CurrencyModel) {
        return currencyModelMapper.selectCurrencyModelList(CurrencyModel);
    }

    /**
     * 新增用于初版模板的CURD管理
     *
     * @param currencyModel 用于初版模板的CURD管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertCurrencyModel(CurrencyModel currencyModel) {
        currencyModel.setActive(1);
        currencyModel.setId(SnakeFlowUtil.getUUID());
        currencyModel.setCreateTime(DateUtils.getNowDate());
        currencyModel.setUpdateTime(DateUtils.getNowDate());
        //SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        currencyModel.setOperatorId("少男");
        return currencyModelMapper.insertCurrencyModel(currencyModel);
    }

    /**
     * 修改用于初版模板的CURD管理
     *
     * @param currencyModel 用于初版模板的CURD管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateCurrencyModel(CurrencyModel currencyModel) {
        //SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        currencyModel.setOperatorId("少男");
        currencyModel.setUpdateTime(DateUtils.getNowDate());
        return currencyModelMapper.updateCurrencyModel(currencyModel);
    }

    /**
     * 删除用于初版模板的CURD管理对象
     *
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public RCode deleteCurrencyModelByIds(String id, String strFlag) {
        int idLen;
        String[] ids = id.split(",");
        if ((idLen = ids.length) == 0) {
            return RCode.IMPORT_ERROR;
        }
//        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        CurrencyModel currencyModel = new CurrencyModel();
        currencyModel.setId(id);
        currencyModel.setUpdateTime(DateUtils.getNowDate());
        currencyModel.setOperatorId("少男");
        if (!StringUtils.isBlank(strFlag)) {
            // 启用
            if (Constants.ENABlE_FLAG.equals(strFlag)) {
                if (idLen > 1) {
                    return new RCode(currencyModelMapper.enableCurrencyModelByIds(currencyModel), "");
                }
                return new RCode(currencyModelMapper.enableCurrencyModelById(currencyModel), "");
            }

            // 废用
            if (Constants.DEL_FLAG.equals(strFlag)) {
                int reCount = currencyModelMapper.selectActiveModelCount(id);
                if (reCount > 0) {
                    return RCode.HAS_VALUE;
                }
                if (idLen > 1) {
                    return new RCode(currencyModelMapper.deleteCurrencyModelByIds(currencyModel), "");
                }
                return new RCode(currencyModelMapper.deleteCurrencyModelById(currencyModel), "");
            }
        }
        return RCode.NO_QUERY;
    }
}
